package com.youdoneed.friends.lostChild;

import com.youdoneed.base.application.ResultMessage;
import com.youdoneed.base.common.JWTUtils;
import com.youdoneed.base.common.JsonConverter;
import com.youdoneed.base.pagination.Page;
import com.youdoneed.base.pagination.Paging;
import com.youdoneed.friends.model.LostChild;
import com.youdoneed.friends.model.LostChildClueRecord;
import com.youdoneed.friends.vo.LostChildClueParamVo;
import com.youdoneed.friends.vo.LostChildClueVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Authorization;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * Created by lid on 2017/12/18 0018.
 */
@RestController
@RequestMapping("/lostChild")
@Api(description = "失踪儿童控制类")
public class LostChildController {

    private static final Logger logger = LoggerFactory.getLogger(LostChildController.class);

    @Autowired
    private LostChildService lostChildService;

    @RequestMapping(value = "/updateInBatch", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "批量更新失踪儿童数据", value = "批量更新失踪儿童数据")
    public ResultMessage<?> updateInBatch() {
        lostChildService.updateInBatch();
        return new ResultMessage<>();
    }

    @RequestMapping(value = "/findChildByNoOrSex", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "后台查找失踪儿童数据", value = "后台查找失踪儿童数据")
    public ResultMessage<Page<LostChild>> findChildByNoOrSex(Paging paging, String no, String sex){
        Page<LostChild> listLostChild = lostChildService.findChildByNoOrSex(paging, no, sex);
        return new ResultMessage<>(listLostChild).toSuccess();
    }

    @RequestMapping(value = "/deleteLostChild", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "后台删除失踪儿童数据", value = "后台删除失踪儿童数据")
    public ResultMessage<?> deleteLostChild(String id){
        lostChildService.deleteLostChild(id);
        return new ResultMessage<>().toSuccess("删除失踪儿童成功");
    }

    @RequestMapping(value = "/getLostChildDetail", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "后台查找失踪儿童详情数据", value = "后台查找失踪儿童详情数据")
    public ResultMessage<LostChild> getLostChildDetail(String id){
        LostChild child = lostChildService.getLostChildDetail(id);
        return new ResultMessage<>(child).toSuccess("删除失踪儿童成功");
    }

    /**
     * 保存失踪儿童的线索记录
     * @return
     * @author lid
     * @date 2017.12.20
     */
    @ResponseBody
    @RequestMapping(value = "/saveLostChildClue", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "保存失踪儿童的线索记录", value = "保存失踪儿童的线索记录")
    public ResultMessage<?> saveLostChildClue(@RequestHeader(value = "Authorization") String token,
                                              @RequestBody LostChildClueParamVo vo){
        String userId = JWTUtils.getTokenUserId(token);
        if (StringUtils.hasText(userId)) {
            logger.info("userId:" + userId);
        }

        if(StringUtils.hasText(token)){
            logger.info("token:"+token);
        }
        if(null != vo){
            logger.info("vo:"+ JsonConverter.toJson(vo));
        }

        lostChildService.insertLostChildClueRecord(userId, vo.getLostChildId(),
                vo.getPhotoUrls(), vo.getMessage());
        return new ResultMessage<>().toSuccess("保存失踪儿童线索成功");
    }

    /**
     * 保存失踪儿童的线索记录
     * @return
     * @author lid
     * @date 2017.12.20
     */
    @ResponseBody
    @RequestMapping(value = "/saveLostClue", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "保存失踪儿童的线索记录（新的参数形式）", value = "保存失踪儿童的线索记录（新的参数形式）")
    public ResultMessage<?> saveLostClue(@RequestHeader(value = "Authorization") String token,
                                         String lostChildId,
                                         String photoUrls,
                                         String message){
        String userId = JWTUtils.getTokenUserId(token);
        lostChildService.saveLostChildClue(userId, lostChildId,
                photoUrls, message);
        return new ResultMessage<>().toSuccess("保存失踪儿童线索成功");
    }

    @RequestMapping(value = "/findLostChildClueRecord", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "后台查找失踪儿童线索数据", value = "后台查找失踪儿童线索数据")
    public ResultMessage<Page<LostChildClueRecord>> findLostChildClueRecord(Paging paging){
        Page<LostChildClueRecord> listLostChildClueRecord = lostChildService.findLostChildClueRecord(paging);
        return new ResultMessage<>(listLostChildClueRecord).toSuccess();
    }

    @RequestMapping(value = "/getLostChildClueDetail", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "查找失踪儿童线索详细数据", value = "查找失踪儿童线索详细数据")
    public ResultMessage<LostChildClueVo> getLostChildClueDetail(String recordId){
        LostChildClueVo vo = lostChildService.getLostChildClueDetail(recordId);
        return new ResultMessage<>(vo).toSuccess();
    }

}
